﻿using System.Collections.Generic;
using Gp.GCX;
using Gp.Scripts.Core;
using QFramework;


namespace Gp.Scripts.Core {
    public class ShowTutorialWindowEventArgs {
        public string Key;

        public ShowTutorialWindowEventArgs(string tutorialKey) {
            Key = tutorialKey;
        }
    }
    
    [AutoLoad]
    public class TutorialSystem : BaseGameSystem {
        private List<IUnRegister> _ls = new();

        protected override void OnInit() {
            MainArchitecture.Interface.RegisterModel(new TutorialModel());
            // this.RegisterEvent<Event_BattleStart>(OnBattleStart);
            // this.RegisterEvent<Event_BattleEnd>(OnBattleEnd);
        }



        private void OnBattleStart(Event_BattleStart obj) {
            TryShowTutorial(TK.Resource);
            _ls.Add(this.RegisterEvent<Event_Unit.Select>(OnUnitSelect));
            _ls.Add(this.RegisterEvent<UnitAttackResultEvent>(_ => {
                TryShowTutorial(TK.DamageCalculation);
            }));
        }
        

        private void OnBattleEnd(Event_BattleEnd obj) {
            _ls.ForEach(x => x.UnRegister());
            _ls.Clear();
        }

        private void OnUnitSelect(Event_Unit.Select obj) {
            // 当单位选中时
            if (obj.Unit == null) {
                return;
            }


            switch (obj.Unit.ComEquip.CurWeapon.TypeCode) {
                case WeaponTypeCode.GreatSword:
                    TryShowTutorial(TK.GSword);
                    break;
                case WeaponTypeCode.LightSword:
                    TryShowTutorial(TK.LSword);
                    break;
                case WeaponTypeCode.Stick:
                    TryShowTutorial(TK.MagicWeapon);
                    break;
                case WeaponTypeCode.Rife:
                    TryShowTutorial(TK.RifeWeapon);
                    break;
            }
        }


        private bool TryShowTutorial(string tutorialKey) {
            TutorialModel model = this.GetModel<TutorialModel>();
            if (model.TryTriggered(tutorialKey)) {
                this.SendEvent(new ShowTutorialWindowEventArgs (tutorialKey));
                return true;
            }

            return false;
        }
        
        


        private static class TK {
            public const string LSword = "weapon_lsword";
            public const string GSword = "weapon_gsword";
            public const string RifeWeapon = "weapon_rife";
            public const string MagicWeapon = "weapon_magic";
            public const string Resource = "basic_resource";
            public const string DamageCalculation = "damage_calculation";
        }
    }
}